package com.wnxy.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.admin.dto.AuthorizationDto;
import com.wnxy.admin.entity.RolePermission;
import com.wnxy.admin.mapper.RolePermissionMapper;
import com.wnxy.admin.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * <p>
 * 角色权限中间表 服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    private final RolePermissionMapper rolePermissionMapper;
    public RolePermissionServiceImpl(RolePermissionMapper rolePermissionMapper){
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public void authorization(AuthorizationDto authorizationDto) {
        //先清理一次
        rolePermissionMapper.delete(Wrappers.lambdaQuery(RolePermission.class)
                .eq(RolePermission::getRoleId,authorizationDto.getId()));
        //加入数据库
        authorizationDto.getAuthorizationDatas().forEach(e ->{
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(e);
            rolePermission.setRoleId(authorizationDto.getId());
            rolePermissionMapper.insert(rolePermission);
        });
    }
}
